package me.lcc.demo.chapter1.topic4;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 将Java文件转换为Unicode输出
 */
public class JavaContent2Unicode {
	public static void main(String[] args) throws Exception {
		// 通过参数来获取制定的Java文件
		File jfile = new File(args[0]);
		if(!jfile.exists()) {
			System.out.println("文件路径未找到：" + jfile.getAbsolutePath());
			return;
		}
		
		// 读取文本内容
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(jfile)));

		String has = null;
		while((has = br.readLine()) != null) {
			toUnicode(has);
		}
		
		br.close();
	}
		
	/**
	 * 
	 * @param gbString
	 * @return
	 */
	public static void toUnicode(final String line) {
        for (char ch : line.toCharArray()) {
			System.out.print(format(ch));
		}
        System.out.println();
    }
	
	/**
	 * 格式化为Unicode
	 * @param ch
	 * @return
	 */
	public static String format(char ch) {
		char[] ts = {'\\','u','0','0','0','0'};
		char[] cs = Integer.toHexString(ch).toCharArray();
		System.arraycopy(cs, 0, ts, ts.length-cs.length, cs.length);
		
		return String.valueOf(ts);
	}
}
